/**
 * Created by ChenYong on 2017-04-12.
 *
 * 举报列表。
 */

$(function () {
    /** 举报人的 jQuery 对象 */
    var $reportCreatePerson = $("#report_create_person");
    /** 举报人手机号的 jQuery 对象 */
    var $reportCreatePersonPhone = $("#report_create_person_phone");
    /** 处理人的 jQuery 对象 */
    var $reportHandlePerson = $("#report_handle_person");
    /** 举报标题 jQuery 对象 */
    var $reportName = $("#report_name");
    /** 举报来源 jQuery 对象 */
    var $reportSource = $("#report_source");
    /** 举报来源 jQuery 对象 */
    var $reportResult = $("#report_result");
    /** 举报开始日期的 jQuery 对象 */
    var $reportCreateStartDate = $("#report_create_start_date");
    /** 举报结束日期的 jQuery 对象 */
    var $reportCreateEndDate = $("#report_create_end_date");
    /** 清空举报开始日期的 jQuery 对象 */
    var $reportCreateStartDateCloseBtn = $("#report_create_start_date_close_btn");
    /** 清空举报结束日期的 jQuery 对象 */
    var $reportCreateEndDateCloseBtn = $("#report_create_end_date_close_btn");
    /** 处理开始日期的 jQuery 对象 */
    var $reportHandleStartDate = $("#report_handle_start_date");
    /** 处理结束日期的 jQuery 对象 */
    var $reportHandleEndDate = $("#report_handle_end_date");
    /** 清空处理开始日期的 jQuery 对象 */
    var $reportHandleStartDateCloseBtn = $("#report_handle_start_date_close_btn");
    /** 清空处理结束日期的 jQuery 对象 */
    var $reportHandleEndDateCloseBtn = $("#report_handle_end_date_close_btn");
    /** 搜索按钮的 jQuery 对象 */
    var $searchReportBtn = $("#search_report_btn");
    /** 举报模态框的 jQuery 对象 */
    var $reportModal = $("#report_modal");
    /** 举报发红包模态框的 jQuery 对象 */
    var $reportSendredpackModal = $("#report_sendredpack_modal");
    /** 举报发红包按钮的 jQuery 对象 */
    var $reportSendredpackCloseBtn = $reportSendredpackModal.find("button[name=reportSendredpackCloseBtn]");
    /** 举报发红包按钮的 jQuery 对象 */
    var $reportSendredpackBtn = $reportSendredpackModal.find("button[name=reportSendredpackBtn]");
    /** 举报列表的表格，DataTables 的 API 对象 */
    var reportTable = null;
    /** 举报 ID */
    var reportId = null;

    // 选择举报开始日期
    $reportCreateStartDate.datepicker({
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        todayBtn: "linked",
        autoclose: true
    });

    // 选择举报结束日期
    $reportCreateEndDate.datepicker({
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        todayBtn: "linked",
        autoclose: true
    });

    // 清空举报开始日期
    $reportCreateStartDateCloseBtn.click(function () {
        $reportCreateStartDate.val("");
    });

    // 清空举报结束日期
    $reportCreateEndDateCloseBtn.click(function () {
        $reportCreateEndDate.val("");
    });

    // 选择处理开始日期
    $reportHandleStartDate.datepicker({
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        todayBtn: "linked",
        autoclose: true
    });

    // 选择处理结束日期
    $reportHandleEndDate.datepicker({
        format: 'yyyy-mm-dd',
        language: "zh-CN",
        todayBtn: "linked",
        autoclose: true
    });

    // 清空处理开始日期
    $reportHandleStartDateCloseBtn.click(function () {
        $reportHandleStartDate.val("");
    });

    // 清空处理结束日期
    $reportHandleEndDateCloseBtn.click(function () {
        $reportHandleEndDate.val("");
    });

    // 监听举报来源下拉列表搜索
    $reportSource.change(function () {
        searchData();
    });

    // 监听举报结果下拉列表搜索
    $reportResult.change(function () {
        searchData();
    });

    // 点击搜索按钮
    $searchReportBtn.click(function (e) {
        searchData();
    });

    // 监听举报模态框打开
    $reportModal.on("shown.bs.modal", function () {
        // 将滚动条滚动到顶部
        $reportModal.find(".modal-body").scrollTop("0px");
    });

    // 监听举报模态框关闭
    $reportModal.on("hidden.bs.modal", function () {
        $reportModal.find(".modal-body").html("");
    });

    // 监听举报发普通红包模态框打开
    $reportSendredpackModal.on("shown.bs.modal", function () {
        // 将滚动条滚动到顶部
        $reportSendredpackModal.find(".modal-body").scrollTop("0px");
    });

    // 监听举报发普通红包模态框关闭
    $reportSendredpackModal.on("hidden.bs.modal", function () {
        reportId = null;

        $reportSendredpackModal.find(".modal-body").html("");
    });

    // 点击查看举报
    $(document).on("click", ".view_report", function () {
        var $this = $(this);
        var id = $this.data("id");

        $.myAjax({
            url: "web/report/detail.html",
            modal: true,
            dataType: "html",
            showHtml: false,
            data: {
                id: $this.data("id")
            },
            successFn: function (data) {
                $reportModal.find(".modal-body").html(data);

                $reportModal.modal("show");
            }
        });
    });

    // 点击处理举报
    $(document).on("click", ".handle_report", function () {
        var $this = $(this);
        var id = $this.data("id");
        var reportResult = $this.data("report-result");
        var msg = "";

        // 核实中
        if (reportResult == '2') {
            msg = "确定【核实】此举报吗？";

            // 已核实
        } else if (reportResult == '3') {
            msg = "确定【核实通过】此举报吗？";

            // 已关闭
        } else if (reportResult == '4') {
            msg = "确定【核实不通过】此举报吗？";

            // 已发放
        } else if (reportResult == '5') {
            msg = "确定发奖金给此举报人吗？";
        }

        $.showConfirmModal({
            msg: msg,
            confirmFn: function () {
                $.myAjax({
                    url: "web/report/update.json",
                    type: "post",
                    modal: true,
                    data: {
                        id: id,
                        reportResult: reportResult
                    },
                    successFn: function (data) {
                        searchData();
                    }
                });
            }
        });
    });

    // 点击打开发红包的页面
    $(document).on("click", ".report_money", function () {
        var $this = $(this);

        reportId = $this.data("id");

        $.myAjax({
            url: "web/report/sendredpack.html",
            modal: true,
            dataType: "html",
            showHtml: false,
            data: {
                id: reportId
            },
            successFn: function (data) {
                $reportSendredpackModal.find(".modal-body").html(data);

                $reportSendredpackModal.modal("show");
            }
        });
    });

    // 点击发红包
    $reportSendredpackBtn.click(function () {
        var $this = $(this);
        // 举报红包金额单选按钮的 jQuery 对象
        var $reportRedpackAmountCode = $reportSendredpackModal.find("input[name=reportRedpackAmountCode]:checked");
        // 举报红包备注的 jQuery 对象
        var $reportRedpackRemark = $reportSendredpackModal.find("textarea[name=reportRedpackRemark]");

        if ($.isBlank($reportRedpackAmountCode.val())) {
            $.toast({
                top: 5,
                msg: "请选择红包金额",
                type: "error",
                close: 3000
            });

            return;
        }

        if ($.isBlank($reportRedpackRemark.val())) {
            $.toast({
                top: 5,
                msg: "请输入红包备注",
                type: "error",
                close: 3000
            });

            $reportRedpackRemark.focus();

            return;
        }

        if ($reportRedpackRemark.val().length > 50) {
            $.toast({
                top: "5",
                msg: "红包备注的长度不能超过50个字",
                type: "error",
                close: 3000
            });

            $reportRedpackRemark.focus();

            return;
        }

        $.myAjax({
            url: "web/report/sendredpack.json",
            type: "post",
            submitBtn: $this,
            submitMsg: "正在发红包",
            data: {
                id: reportId,
                remark: $reportRedpackRemark.val(),
                amountCode: $reportRedpackAmountCode.val(),
            },
            beforeSendFn: function () {
                $reportSendredpackCloseBtn.attr({"disabled": "disabled"});
            },
            completeFn: function () {
                $reportSendredpackCloseBtn.removeAttr("disabled");
            },
            successFn: function (data) {
                $.toast({
                    top: 5,
                    msg: "发红包成功",
                    type: "success",
                    close: 10000
                });

                $reportSendredpackModal.modal("hide");

                searchData();
            },
            successErrorFn: function (data) {
                $reportSendredpackModal.find(".modal-body").find(".sendredpack-result").html(data.resultMsg);
            }
        });
    });

    // 展示举报表格
    reportTable = $("#report_table").myDataTable({
        url: "web/report/list.json",
        searchBtn: $searchReportBtn,
        sScrollY: "450px",
        sScrollX: true,
        sScrollXInner: "100%",
        aaSorting: [[8, "desc"]],
        aLengthMenu: [10, 30, 50, 100],
        aoColumns: [{
            mData: "checkbox",
            sTitle: "<label class='pos-rel' title='全选'><input type='checkbox' class='ace'><span class='lbl'></span></label>",
            sWidth: "50px",
            sClass: "center",
            bSortable: false,
            bSearchable: false,
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                $cell.html("<label class='pos-rel'><input type='checkbox' class='ace' data-id='{0}'><span class='lbl'></span></label>".format(rowData.id));
            }
        }, {
            mData: "reportName",
            sName: "reportName",
            sTitle: "举报标题"
        }, {
            mData: "reportCreatePerson",
            sName: "reportCreatePerson",
            sTitle: "举报人",
            sWidth: "100px"
        }, {
            mData: "reportCreatePersonPhone",
            sName: "reportCreatePersonPhone",
            sTitle: "举报人手机号",
            sWidth: "130px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                if ($.isNotBlank(rowData.reportCreatePersonPhone)) {
                    $cell.html("<span class='c-red f-s-20'>{0}</span>".format(rowData.reportCreatePersonPhone))
                }
            }
        }, {
            mData: "openid",
            sName: "openid",
            sTitle: "举报人openid",
            sWidth: "200px"
        }, {
            mData: "reportHandlePerson",
            sName: "reportHandlePerson",
            sTitle: "处理人",
            sWidth: "100px"
        }, {
            mData: "reportSource",
            sName: "reportSource",
            sTitle: "举报来源",
            sWidth: "100px"
        }, {
            mData: "reportMoney",
            sName: "reportMoney",
            sTitle: "举报奖金",
            sWidth: "100px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                // 单元格的 jQuery 对象
                var $cell = $(cell);

                if ($.isNotBlank(rowData.reportMoney)) {
                    $cell.html("<span class='c-red m-r-5'>{0}</span>元".format(rowData.reportMoney))
                }
            }
        }, {
            mData: "reportCreateTime",
            sName: "reportCreateTime",
            sTitle: "举报时间",
            sWidth: "130px"
        }, {
            mData: "reportHandleTime",
            sName: "reportHandleTime",
            sTitle: "处理时间",
            sWidth: "130px"
        }, {
            mData: "reportResult",
            sName: "reportResult",
            sTitle: "举报结果",
            sWidth: "100px",
            sDefaultContent: "",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                var $cell = $(cell);

                // 已举报
                if (rowData.reportResult == "1") {
                    $cell.html("<span class='label label-lg b-c-gray'>已举报</span>");

                    // 核实中
                } else if (rowData.reportResult == "2") {
                    $cell.html("<span class='label label-lg b-c-orange'>核实中</span>");

                    // 已核实
                } else if (rowData.reportResult == "3") {
                    $cell.html("<span class='label label-lg b-c-blue'>已核实</span>");

                    // 已关闭
                } else if (rowData.reportResult == "4") {
                    $cell.html("<span class='label label-lg b-c-red'>已关闭</span>");

                    // 已发放
                } else if (rowData.reportResult == "5") {
                    $cell.html("<span class='label label-lg b-c-green'>已发放</span>");

                    // 其他
                } else {
                    $cell.html(rowData.reportResult);
                }
            }
        }, {
            mData: "operator",
            sTitle: "操作",
            sWidth: "120px",
            bSortable: false,
            bSearchable: false,
            sDefaultContent: "<div class='hidden-sm hidden-xs action-buttons'></div>",
            fnCreatedCell: function (cell, cellData, rowData, row, col) {
                // 单元格的 jQuery 对象
                var $cell = $(cell);

                $cell.find("div.action-buttons").append("<a class='c-blue view_report' title='查看' data-id='{0}' href='javascript:void(0);'><i class='ace-icon iconfont icon-eye bigger-200'></i></a>".format(rowData.id));

                // 已举报
                if (rowData.reportResult == "1") {
                    $cell.find("div.action-buttons").append("<a class='c-gray handle_report' title='核实' data-id='{0}' data-report-result='2' href='javascript:void(0);'><i class='ace-icon iconfont icon-examine bigger-200'></i></a>".format(rowData.id));

                    // 核实中
                } else if (rowData.reportResult == "2") {
                    $cell.find("div.action-buttons").append("<a class='c-green handle_report' title='核实通过' data-id='{0}' data-report-result='3' href='javascript:void(0);'><i class='ace-icon iconfont icon-right bigger-200'></i></a>".format(rowData.id));
                    $cell.find("div.action-buttons").append("<a class='c-red handle_report' title='核实不通过' data-id='{0}' data-report-result='4' href='javascript:void(0);'><i class='ace-icon iconfont icon-error bigger-200'></i></a>".format(rowData.id));

                    // 已核实，只有【超级管理员】、【廖昆】才能发红包
                } else if (((globalLoginid == "administrator") ||
                    (globalLoginid == "qzlkun")) &&
                    (rowData.reportResult == "3")) {
                    $cell.find("div.action-buttons").append("<a class='c-red report_money' title='发奖金' data-id='{0}' data-report-result='5' href='javascript:void(0);'><i class='ace-icon iconfont icon-rmb bigger-200'></i></a>".format(rowData.id));
                }
            }
        }]
    });

    /**
     *
     * 搜索数据。
     *
     */
    function searchData() {
        // 举报人
        if ($.isNotBlank($reportCreatePerson.val()) && ($reportCreatePerson.val().length > 50)) {
            $.toast({
                top: 5,
                msg: "举报人的长度不能大于50个字",
                type: "warn",
                close: 3000
            });

            return;
        }

        // 举报人手机号
        if ($.isNotBlank($reportCreatePersonPhone.val()) && !phoneRE.test($reportCreatePersonPhone.val())) {
            $.toast({
                top: 5,
                msg: "举报人手机号格式不正确",
                type: "warn",
                close: 3000
            });

            return;
        }

        // 处理人
        if ($.isNotBlank($reportHandlePerson.val()) && ($reportHandlePerson.val().length > 50)) {
            $.toast({
                top: 5,
                msg: "处理人的长度不能大于50个字",
                type: "warn",
                close: 3000
            });

            return;
        }

        // 举报标题
        if ($.isNotBlank($reportName.val()) && ($reportName.val().length > 500)) {
            $.toast({
                top: 5,
                msg: "举报标题的长度不能大于500个字",
                type: "warn",
                close: 3000
            });

            return;
        }

        // 举报开始日期必须小于举报结束日期
        if ($.isNotBlank($reportCreateStartDate.val()) && $.isNotBlank($reportCreateEndDate.val())) {
            if (Number($reportCreateStartDate.val().replaceAll("-", "")) > Number($reportCreateEndDate.val().replaceAll("-", ""))) {
                $.toast({
                    top: 5,
                    msg: "举报开始日期应小于举报结束日期",
                    type: "warn",
                    close: 3000
                });

                return;
            }
        }

        // 处理开始日期必须小于处理结束日期
        if ($.isNotBlank($reportHandleStartDate.val()) && $.isNotBlank($reportHandleEndDate.val())) {
            if (Number($reportHandleStartDate.val().replaceAll("-", "")) > Number($reportHandleEndDate.val().replaceAll("-", ""))) {
                $.toast({
                    top: 5,
                    msg: "处理开始日期应小于处理结束日期",
                    type: "warn",
                    close: 3000
                });

                return;
            }
        }

        $reportName.val($.trim($reportName.val()));
        $reportHandlePerson.val($.trim($reportHandlePerson.val()));
        $reportCreatePerson.val($.trim($reportCreatePerson.val()));
        $reportCreatePersonPhone.val($.trim($reportCreatePersonPhone.val()));

        // 搜索开始…
        if ($.isNotNull(reportTable)) {
            reportTable.myAjaxData = {
                reportName: $reportName.val(),
                reportSource: $reportSource.val(),
                reportResult: $reportResult.val(),
                reportHandlePerson: $reportHandlePerson.val(),
                reportCreatePerson: $reportCreatePerson.val(),
                reportCreateEndDate: $reportCreateEndDate.val(),
                reportHandleEndDate: $reportHandleEndDate.val(),
                reportCreateStartDate: $reportCreateStartDate.val(),
                reportHandleStartDate: $reportHandleStartDate.val(),
                reportCreatePersonPhone: $reportCreatePersonPhone.val()
            };

            // 刷新举报表格（分页重置）
            reportTable.ajax.reload();
        }
    }
});